package com.file.controller.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.file.domain.FileTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadLocalRandom;

@Slf4j
public class FileUploadUtils {
    //将文件上传
    public static String upload(MultipartFile file) {
        FileTypeEnum fileType = classificate(file);

        //准备日期格式/yymm/dd
        LocalDate now = LocalDate.now();
        String yymm = now.format(DateTimeFormatter.ofPattern("yyMM"));
        String dd = now.format(DateTimeFormatter.ofPattern("dd"));

        //random(1~1000)+毫秒级时间戳及毫秒级时间戳
        int random = ThreadLocalRandom.current().nextInt(1, 1001);
        long ms = System.currentTimeMillis();
        String md5 = DigestUtils.md5DigestAsHex((String.valueOf(random)+String.valueOf(ms)).getBytes());

        //准备w*h
        String wxh = "";
        if (fileType.getCode() == 1) {
            try (InputStream inputStream = file.getInputStream()) {
                BufferedImage image = ImageIO.read(inputStream);
                if (image != null) {
                    int width = image.getWidth();
                    int height = image.getHeight();
                    wxh = "_" + width + "x" + height;
                }
            } catch (Exception e) {
                log.warn(e.getMessage());
            }
        }
//        jeGfc1PVRD0?kRSqx!e7vvAP5(sCOA{j

        //组合文件名
        String fileName = fileType.getPath() + "/" + yymm + "/" + dd + "/" + md5 + wxh + "." + getSuffix(file);
        log.info(fileName);
        //配置阿里云仓库
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5t8dkxSxumzuxyaYhqqt";
        String accessKeySecret = "CjmKrLgSrhWJPEuISelOvoKePHKZL3";
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.putObject("practice-mazheng", fileName, file.getInputStream());
        } catch (OSSException | ClientException | IOException e) {
            return "插入失败";
        }
        ossClient.shutdown();

        String url = "https://" + "practice-mazheng" + "." + endpoint + "/" + fileName;
        return url;
    }

    //获取文件分类
    public static FileTypeEnum classificate(MultipartFile file) {
        String fileOriginalName = file.getOriginalFilename();

        //准备路径分类 /image /video /file
        String suffix = null;
        if (fileOriginalName != null) {
            suffix = fileOriginalName.substring(fileOriginalName.lastIndexOf(".") + 1).toLowerCase();
        }
        return FileTypeEnum.fromSuffix(suffix);
    }

    //获取文件后缀
    public static String getSuffix(MultipartFile file) {
        String fileOriginalName = file.getOriginalFilename();

        //准备路径分类 /image /video /file
        String suffix = null;
        if (fileOriginalName != null) {
            suffix = fileOriginalName.substring(fileOriginalName.lastIndexOf(".") + 1).toLowerCase();
        }
        return suffix;
    }


}
